package org.example.fronted.service.impl;


import org.example.entity.Orders;
import org.example.entity.WebOrders;
import org.example.fronted.mapper.ShowOrderMapper;
import org.example.fronted.service.ShowOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
@Transactional
public class ShowOrderServiceImpl implements ShowOrderService {


    private ShowOrderMapper showOrderMapper;

    public ShowOrderServiceImpl(ShowOrderMapper showOrderMapper){
        this.showOrderMapper=showOrderMapper;
    }


    //获取当前项目路径
    private final String imageServerLocation = "http://localhost/";
    @Override
    public List<WebOrders> getOrdersByStatus(Map ma) {
        List<Orders> orderList = showOrderMapper.selectByMap(ma);

//        ShowOrderMapper orderDao=new ShowOrderDaoImpl();

//        List<Orders> orderList = orderDao.getOrdersByStatus(status, userId)

        List<WebOrders> webOrderList = new ArrayList<>();

        /**
         * map作用是将订单按照订单编号来分类。key是订单编号，value是拥有相同订单编号的订单.
         */
        Map<String, List<Orders>> map = new HashMap<>();

        /**
         * map的作用是存储对应订单的创建日期，key是订单编号，value是订单的创建日期
         */
        Map<String, Date> orderTimeMap = new HashMap<>();

        for(Orders o : orderList) {
            String orderNo = o.getONo(); //订单编号
            Date createTime = o.getOCreateTime(); //订单的创建日期

            //获取照片的路径
            o.setOGoodImg(imageServerLocation + o.getOGoodImg());

            // 获取该订单编号的创建日期
            Date createDate = orderTimeMap.get(orderNo);
            if(null == createDate) {
                orderTimeMap.put(orderNo, createTime);
            }

            // 获取该订单编号对应的所有的订单
            List<Orders> orders = map.get(orderNo);
            if(null == orders) {  //该订单编号没有对应的订单
                orders = new ArrayList<>();
                orders.add(o);
                map.put(orderNo, orders);
            }else { //该订单编号有订单了
                orders.add(o);
            }
        }


        map.forEach((key, value) -> {
            WebOrders webOrder = new WebOrders();

            webOrder.setOrderNo(key);
            webOrder.setOrders(value);

            Date createTime = orderTimeMap.get(key);  //订单的创建日期

            webOrder.setCreateTime(orderTimeMap.get(key));

            webOrderList.add(webOrder);
        });

        return webOrderList;
    }

    //取消订单
    @Override
    public void cancelOrder(String oNo) {

//        showOrderMapper
//        ShowOrderDao orderDao = new ShowOrderDaoImpl();
//        orderDao.cancelOrder(oNo);
    }

    @Override
    public boolean cancelOrderByoNo(String oNo) {

//        String sql = "update orders set o_status=2 where o_no="+oNo;
        try {
            showOrderMapper.cancelOrderByoNo(oNo);
//            Integer.parseInt("abc");
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public List<Orders> payByoNo(String oNo) {

        List<Orders> ordersList = showOrderMapper.payByoNo(oNo);
//        ShowOrderDao showOrderDao=new ShowOrderDaoImpl();
//        List<Orders> ordersList = showOrderDao.payByoNo(oNo);
//        return ordersList;
        return ordersList;
    }

    //未支付订单继续支付，成功
    @Override
    public boolean confirmPay(String oNo) {
        try{
            showOrderMapper.confirmPay(oNo);
            return true;
        }catch (Exception e){
            return false;
        }

    }

    public boolean checkPay(String oNo){
        List<Orders> ordersList = showOrderMapper.checkPay(oNo);

        if (ordersList.size()>0){
            return true;
        }else {
            return false;
        }
    }


    public boolean update(String oNo){
        boolean flag = showOrderMapper.update(oNo);
        if (flag){
            return true;
        }else {
            return false;
        }
    }
}
